// 使用 Mock
var Mock = require('mockjs')
// 列表模拟数据生成
//'userList|55': 数组的长度为55
const data = Mock.mock({
  'userList|55': [
    {
      id: '@increment',
      name: '@cname()',
      'phone|11': '@integer(0,9)',
      address: '@city(true)',
      date: '@date(yyyy-MM-dd)'
    }
  ]
})
// 处理get请求的url参数  "/api/api/mock/userList?pageSize=10&currentPage=1"
 const getQuery = (url, name) => {
    const isFind = url.indexOf(name) !== -1
    if (isFind) {
      const queryArr = url.split('?')[1].split('&')
      for (let i = 0; i < queryArr.length; i++) {
        const result = queryArr[i].split('=')
        if (result[0] === name) {
          return result[1]
       }
     }
   }
   return null
 }
// 生成 查询 功能的接口
Mock.mock(RegExp('mock/userList'), 'get', (options) => {
  console.log(options)
  // 根据url解析出pageSize的值
    const pageSize = getQuery(options.url, 'pageSize')
    	// 根据url解析出currentPage的值
    const currentPage = getQuery(options.url, 'currentPage') 
  /**
 7    * 对返回的数组进行处理 (即实现分页效果)
 8    * 1.获取起始值  : pageSize=1 currentPage=10 即 0(索引)  
 9      计算公式 : (pageSize-1)*currentPage
 10    * 2.获取终止值  : pageSize=1 currentPage=10 即 9(索引)  
 11      计算公式 : pageSize*currentPage (不-1,是因为使用splice进行截取;)
 12    * 3.如当前页码超出总页码,则返回 [] ;  
 13      计算公式 : totalPage=Math.ceil(data.length/currentPage)
 14    */
    const startIndex = (currentPage - 1) * pageSize
    const endIndex = pageSize * currentPage
    const totalPage = Math.ceil(data.userList.length / pageSize)
  
    const newDataArr =
      currentPage > totalPage ? [] : data.userList.slice(startIndex, endIndex)
  
    return {
      code: 200,
      success: true,
      message: '获取用户列表成功',
      data: newDataArr,
      totalPage: data.userList.length
   }
})
Mock.mock(RegExp('mock/addUser'), 'post', (options) => {
  console.log(options)
    const params = JSON.parse(options.body)
      console.log(params)
    data.userList.unshift(
      Mock.mock({
        id: '@increment',
        name: params.name,
        address: params.address,
        phone:params.phone,
        date: params.date
      })
   )
   console.log(data.userList)
   return {
     code: 200,
     success: true,
     message: '用户添加成功'
   }
  })

  Mock.mock(RegExp('mock/editUser'), 'post', (options) => {
  const params = JSON.parse(options.body)
  for (let i = 0; i < data.userList.length; i++) {
    if (data.userList[i].id === params.id) {
      data.userList[i] = params
    }
  }
  return {
    code: 200,
    success: true,
    message: '用户修改成功'
  }
})
Mock.mock(RegExp('mock/deleteUser'), 'post', (options) => {
  const params = JSON.parse(options.body)
  console.log(params.id)
  const deleteIndex = data.userList.findIndex((item) => {
    return item.id === params.id
  })
  data.userList.splice(deleteIndex, 1)
console.log(data.userList)
  return {
    code: 200,
    success: true,
    message: '用户删除成功'
  }
})
let token=""
Mock.mock('api/login','post',(options)=>{
  const params = JSON.parse(options.body)
  let userName=params.userName
  let password=params.password
  token='you_token_112233'+userName
  if(userName=='admin'&&password=="123456"){
    return {
      code: 200,
      success: true,
      message: '登录成功',
      data: {
        token:token,
        userInfo:{
          name:'admin'
        }
      },
   }
  }else{
    return {
      code: 400,
      success: false,
      message: '用户名或密码错误',
      data:null
   }
  }
})
Mock.mock('api/logout','post',(options)=>{
  token=''
    return {
      code: 200,
      success: true,
      message: '退出成功',
      data: null,
   }

})
